package com.zhuozhou.jdyc.main.user.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.base.BaseViewModel;
import com.zhuozhou.jdyc.databinding.FeedbackBinding;
import com.zhuozhou.jdyc.main.base.adapter.ImageAddAdapter;
import com.zhuozhou.jdyc.util.log.LogUtil;
import com.zhuozhou.jdyc.util.media.MediaBuilder;
import com.zhuozhou.jdyc.util.media.MediaHelper;
import com.zhuozhou.jdyc.util.media.MediaTypeEnum;
import com.zhuozhou.jdyc.wight.dialog.OpenImageDialog;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;


/**
 * Created by 方舟 on 2018/1/22.
 * 问题反馈
 */
public class FeedBackActivity extends BaseActivity<BaseViewModel, FeedbackBinding> implements ImageAddAdapter.ImageViewAddListener,
        ImageAddAdapter.ImageViewClearListener {
    private ImageAddAdapter imageAddAdapter;
    private MediaHelper mediaHelper;

    @Override
    protected int getLayoutId() {
        return R.layout.feedback;
    }

    @Override
    public String setTitleBar() {
        return "问题反馈";
    }

    @Override
    public void initView() {
        mediaHelper = new MediaBuilder(this, this)
                .setImageAlbumCount(9)
                .setImageQualityCompress(200)
                .builder();
        mediaHelper.getMutableLiveData().observe(this, mediaBean -> {
            if (mediaBean.getMediaType() == MediaTypeEnum.IMAGE.getMediaType()) {
                imageAddAdapter.getList().addAll(mediaBean.getMediaList());
                imageAddAdapter.notifyDataSetChanged();
                mediaHelper.setCurrentImageCount(imageAddAdapter.getList().size());
            }
        });
        mediaHelper.getMutableLiveDataCompress().observe(this, mediaBean -> {
            LogUtil.show("压缩完成：" + mediaBean.toString());
        });
        imageAddAdapter = new ImageAddAdapter(this);
        imageAddAdapter.setImageViewAddListener(this);
        imageAddAdapter.setImageViewClearListener(this);
        binding.feedBackRecyclerView.setLayoutManager(new FullyGridLayoutManager(this, 4) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        binding.feedBackRecyclerView.setAdapter(imageAddAdapter);
        binding.feedBackBtn.setOnClickListener(v -> {
            mediaHelper.startCompressImage(imageAddAdapter.getList());
        });

    }

    @Override
    public void initData(Bundle bundle) {

    }

    @Override
    public void imgClear(View view, int position) {
        imageAddAdapter.getList().remove(position);
        imageAddAdapter.notifyDataSetChanged();
        mediaHelper.setCurrentImageCount(imageAddAdapter.getList().size());
    }

    @Override
    public void imgAdd(View view) {
        new OpenImageDialog(context)
                .setMediaType(OpenImageDialog.CAMERA_ALBUM)
                .setOnOpenImageClickListener(mediaHelper)
                .builder()
                .show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mediaHelper.onActivityResult(requestCode, resultCode, data);
    }

    public static void show(Context context) {
        Intent intent = new Intent(context, FeedBackActivity.class);
        context.startActivity(intent);
    }

}
